package com.example.shopsystem.service;

import com.example.shopsystem.dao.*;
import com.example.shopsystem.entity.*;
import com.example.shopsystem.entity.dto.AdminAndPower;
import com.example.shopsystem.entity.dto.QueryReturnDto;
import com.example.shopsystem.entity.vo.AmdPowVo;
import com.example.shopsystem.entity.vo.OrderDetailVo;
import com.example.shopsystem.entity.vo.PurchaseOrder;
import com.example.shopsystem.entity.vo.ReturnVo;
import com.example.shopsystem.service.Impl.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class AdminServiceImpl implements AdministratorService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    GoodMapper goodMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BusinessMapper businessMapper;
    @Autowired
    BusinessServiceImpl businessService;

    @Override
    public Boolean addItem(Good good) {
        if (!goodMapper.addItem(good)) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean modifyItem(Good good) {
        if (!goodMapper.modifyItem(goodCompletion(good))) {
            return false;
        }
        return true;
    }

    public Good goodCompletion(Good good) {//补全good
        Good good1 = goodMapper.viewItem(String.valueOf(good.getId()));
        if (good.getName() == null) {
            good.setName(good1.getName());
        }
        if (String.valueOf(good.getPrice()) == null) {
            good.setPrice(good1.getPrice());
        }
        if (String.valueOf(good.getCount()) == null) {
            good.setCount(good1.getCount());
        }
        if (good.getDetail() == null) {
            good.setDetail(good1.getDetail());
        }
        return good;
    }

    @Transactional
    @Override
    public Boolean adminRegister(Administrator administrator) {
        if (adminMapper.queryAdmin(administrator.getId()).getPwd() != null) {
            return false;
        }
        adminMapper.register(administrator);
//        System.out.println(administrator);
        if (administrator.getType() == 1) {
            for (int i = 1; i < 15; i++) {
                adminMapper.givePower(administrator.getPhone(), i);

//                System.out.println(i);
            }
        } else if (administrator.getType() == 2) {
            adminMapper.givePower(administrator.getPhone(), 9);
            adminMapper.givePower(administrator.getPhone(), 11);
        } else if (administrator.getType() == 3) {
            adminMapper.givePower(administrator.getPhone(), 7);
            adminMapper.givePower(administrator.getPhone(), 8);
            adminMapper.givePower(administrator.getPhone(), 9);
        }
        return true;
    }

    @Override
    public Boolean admLogin(Administrator administrator) {
        Administrator administrator1 = adminMapper.queryAdmin(administrator.getId());
        if (administrator1.getState() == 2) {
            return false;
        }
//        System.out.println(administrator);
//        System.out.println(administrator1);
        if (!administrator1.getPwd().equals(administrator.getPwd())) {
            return false;
        }
        return true;
    }

    @Override
    public AdminAndPower queryAdmin(Administrator administrator) {//按id查询
        String adminId = administrator.getId();
        AdminAndPower adminAndPower = new AdminAndPower();
        adminAndPower.setAdministrator(adminMapper.queryAdmin(adminId));
        adminAndPower.setPowerList(adminMapper.queryAdminPow(adminId));
        return adminAndPower;
    }

    public List<AdminAndPower> queryAdminName(Administrator administrator) {//按名字查询
        List<Administrator> administratorList = adminMapper.queryAdminName(administrator.getName());
//        System.out.println(administratorList+"1");
        List<AdminAndPower> adminAndPowersList = new LinkedList<>();
        for (Administrator a : administratorList) {
            AdminAndPower adminAndPower = new AdminAndPower();
            adminAndPower.setAdministrator(a);
            adminAndPower.setPowerList(adminMapper.queryAdminPow(a.getId()));
//            System.out.println(adminAndPower+"2");
            adminAndPowersList.add(adminAndPower);
        }
//        System.out.println(adminAndPowersList+"3");
        return adminAndPowersList;
    }

    @Override
    public List<Administrator> queryAdminList() {

        return adminMapper.queryAdminList();
    }

    @Override
    public Boolean modifyAdmin(Administrator administrator) {//修改管理员信息
        if (!adminMapper.modifyAdmin(completion(administrator))) {
            return false;
        }

        return true;
    }

    public Administrator completion(Administrator administrator) {
        Administrator administrator1 = adminMapper.queryAdmin(administrator.getId());
        if (administrator.getPhone() == null) {
            administrator.setPhone(administrator1.getPhone());
        }
        if (String.valueOf(administrator.getType()) == null) {
            administrator.setType(administrator1.getType());
        }
        if (String.valueOf(administrator.getState()) == null) {
            administrator.setState(administrator1.getState());
        }
        if (administrator.getName() == null) {
            administrator.setName(administrator1.getName());
        }
        if (administrator.getPwd() == null) {
            administrator.setPwd(administrator1.getPwd());
        }
        return administrator;
    }
    @Transactional
    @Override
    public Boolean modifyAdminPower(AmdPowVo amdPowVo) {//修改管理员权限

        if (amdPowVo.getType() == 1) {//增加权限
            if (!adminMapper.givePower(amdPowVo.getAd_id(), amdPowVo.getPow_id())) {
                return false;
            }
            ;
        } else if (amdPowVo.getType() == 2) {//删除权限
            if (!adminMapper.deletePower(amdPowVo.getAd_id(), amdPowVo.getPow_id())) {
                return false;
            }

        }
        return true;
    }

    @Override
    public Boolean deleteAdmin(String adminId) {//删除管理员
        return null;
    }

    @Override
    public List<BossCost> ViewFinance() {//查看流水
        return null;
    }
//
//    @Override
//    public List<Order> ViewSales(String beginTime,String dieTime) {//查看所有订单
//
//        List<Order> orderList=orderMapper.queryOrder( beginTime,dieTime);
//        return orderList;
//    }

    @Override
    public List<PurchaseOrder> goodList(String beginTime, String dieTime) {//统计所有
        // 商品需要进货的列表
        return orderMapper.PrintPurchaseOrder(beginTime, dieTime);
    }

    @Override
    public OrderDetailVo ViewSales(int orderId) {//查看单个订单
        Order order = orderMapper.queryOneOrder(orderId);
        OrderDetailVo orderDto = new OrderDetailVo();
        orderDto.setOrder(order);
        User user = userMapper.queryUser(order.getUId());
        user.setBalance(0);
        user.setPwd(null);
        user.setMoneyPwd(null);
        orderDto.setUser(user);
        Business business = businessMapper.queryBusiness(order.getBusId());
        business.setMoneyPwd(0);
        business.setPwd(null);
        business.setBalance(0);
        orderDto.setBusiness(business);
        orderDto.setOrderDeailList(orderMapper.queryOrderDeail(String.valueOf(order.getId())));
        return orderDto;
    }

    @Override
    public List<ReturnVo> returnGoodsList(QueryReturnDto queryReturnDto) {//查看退货单
        List<ReturnGood> returnGoodList = orderMapper.queryReturnGood(queryReturnDto.getBeginTime(), queryReturnDto.getDieTime(), queryReturnDto.getState());
        List<ReturnVo> returnVoList = new LinkedList<>();
        for (ReturnGood r : returnGoodList) {
            ReturnVo returnVo = new ReturnVo();
            List<ReturnDetail> returnDetailList = orderMapper.queryReturnDetail(r.getId());
            returnVo.setReturnGood(r);
            returnVo.setReturnDetailList(returnDetailList);
            returnVoList.add(returnVo);
        }
        return returnVoList;
    }

    @Override
    public List<Business> viewBus(int state) {//查看所有自提点
        return businessMapper.viewBusinessList(state);
    }

    @Override
    public Boolean updateBus(Business business) {//管理自提点
        if (!businessService.busModifyInfo(business)) {
            return false;
        }

        return true;
    }
}
